<script setup>
import { ref } from 'vue'
import router from '../../router/router'
import axios from 'axios'
const form = ref({
  source: null,
  description: null,
  institute: null,
  major: null,
  grade: null,
  newDate: null,
  totalTime: null,
  totalScore: null,
  type: null,
  tips: null,
  paperId: null
})
const examDate = ref()
const formaTime = (date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  // 拼接
  return year + '-' + month + '-' + day
}

const onSubmit = () => {
  console.log(form.value)

  examDate.value = formaTime(form.value.newDate)
  console.log(examDate.value)
  axios.get('api/examManagecontroller/examManagePaperId').then(res => {
    form.value.paperId = res.data.paperId + 1
    axios.post('api/examManagecontroller/add', {
      source: form.value.source,
      description: form.value.description,
      institute: form.value.institute,
      major: form.value.major,
      grade: form.value.grade,
      examDate: examDate.value,
      totalTime: form.value.totalScore,
      totalScore: form.value.totalScore,
      type: form.value.type,
      tips: form.value.tips,
      paperId: form.value.paperId
    }).then(res => {
      router.push('/selectExam')
    })
  })
}

const cancel = () => {
  form.value = {}
}
</script>

<template>
<section class="add">
    <el-form   label-width="80px" :model="form">
      <el-form-item label="试卷名称">
        <el-input v-model="form.source"></el-input>
      </el-form-item>
      <el-form-item label="介绍">
        <el-input v-model="form.description"></el-input>
      </el-form-item>
      <el-form-item label="所属学院">
        <el-input v-model="form.institute"></el-input>
      </el-form-item>
      <el-form-item label="所属专业">
        <el-input v-model="form.major"></el-input>
      </el-form-item>
      <el-form-item label="年级">
        <el-input v-model="form.grade"></el-input>
      </el-form-item>
      <el-form-item label="考试日期">
        <el-col :span="11">
          <el-date-picker placeholder="选择日期" v-model="form.newDate" style="width: 100%;"></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="持续时间">
        <el-input v-model="form.totalTime"></el-input>
      </el-form-item>
      <el-form-item label="总分">
        <el-input v-model="form.totalScore"></el-input>
      </el-form-item>
      <el-form-item label="考试类型">
        <el-input v-model="form.type"></el-input>
      </el-form-item>
      <el-form-item label="考生提示">
        <el-input type="textarea" v-model="form.tips"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">立即创建</el-button>
        <el-button type="text" @click="cancel()">取消</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>

<style scoped>
.add {
  padding: 0px 40px;
  width: 400px;
}
</style>
